package com.zwr.sheller.core.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CopyObjectResult;
import com.zwr.sheller.core.common.LSYBusinessException;
import com.zwr.sheller.core.common.LSYExceptionCode;

import java.io.InputStream;

/**
 * @Author 付龙江
 * @Date 2021/12/9 16:22
 * @Version 1.0
 **/
public class OSSUtil {
    /**
     * yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
      */
    private static final String ENDPOINT = "https://oss-cn-chengdu.aliyuncs.com";
    /**
     * 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
      */
    private static final String ACCESS_KEY_ID = "LTAI5t7LxVeG3vFGCkdiLKd7";
    private static final String ACCESS_KEY_SECRET = "uZVlTEEbdJTKea3zQgpZ6stFFbyGvi";
    /**
     * 填写Bucket名称。
      */
    private static final String BUCKET_NAME = "bucket-fulongjiang";

    /**
     * 传入文件路径，该路径基于，不包含Bucket名称：例如exampledir/exampleobject.txt
     *      传入文件输入流，该输入流即为需要上传到云的文件
      */
    public static void upload(String path, InputStream inputStream) {
        OSS ossClient = null;
        try {
            // 创建OSSClient实例
            ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
            ossClient.putObject(BUCKET_NAME, path, inputStream);

        }
        finally {
            // 关闭OSSClient
            if(ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    /**
     * 删除文件
     * @param fileName 文件名
     */
    public static void delete(String fileName){
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        // 删除文件或目录。如果要删除目录，目录必须为空。
        ossClient.deleteObject(BUCKET_NAME, fileName);
        // 关闭OSSClient。
        ossClient.shutdown();
    }

    /**
     * 判断文件是否存在
     * @param fileName 全路径文件名称
     * @return 返回boolean
     */
    public static  boolean fileExist(String fileName){
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        boolean found = ossClient.doesObjectExist(BUCKET_NAME,fileName);
        // 关闭OSSClient。
        ossClient.shutdown();
        return found;
    }

    /**
     * 复制文件到指定路径
     * @param oldFilePath
     * @param newFilePath
     * @return
     */
    public static CopyObjectResult copyFile(String oldFilePath,String newFilePath){
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        //拷贝文件
        CopyObjectResult copyObjectResult = ossClient.copyObject(BUCKET_NAME, oldFilePath, BUCKET_NAME, newFilePath);
        ossClient.shutdown();
        return copyObjectResult;
    }

    /**
     * 复制文件到指定路径同时删除原始文件
     *
     * @param originPath
     * @param destPath
     */
    public static void moveFile(String originPath, String destPath) throws LSYBusinessException {
        if(!fileExist(originPath)) {
            System.out.println("+++++++++++++++++++++++" + originPath);
            System.out.println("+++++++++++++++++++++++" + destPath);
            throw new LSYBusinessException(LSYExceptionCode.PICTURE_NOT_EXIST);
        }
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        ossClient.copyObject(BUCKET_NAME, originPath, BUCKET_NAME, destPath);
        ossClient.deleteObject(BUCKET_NAME, originPath);
        ossClient.shutdown();
    }
}
